/**
 * Created by baijs on 2016/12/13.
 */
var express = require('express');
var moment = require('moment');
var router = express.Router();

var smsSender = require("../utils/topClient/sendSMS");
var mailer = require("../utils/mailer/mailUtil");

router.get('/', function (req, res, next) {
    db.models.Sms.find({telNo: /18911301931/, status: true}).limit(1).exec(function (err, sms) {
        res.send(sms)
    });
});
router.get('/valid-code/:telNo', function (req, res, next) {
    var Sms = db.models.Sms;
    var errMsg = {
        success: false,
        msg: '短信发送错误，请稍后再试！'
    };
    var successMsg = {
        success: true,
        msg: '短信发送成功！'
    };
    db.models.Sms.find({
        telNo: req.params.telNo,
        // status: true,
        invalidTime: {"$gte": new Date()}
    }).limit(1).exec(function (err, sms) {
        if (sms.length > 0) {
            errMsg.msg="短信发送太频繁，请稍后再试！"
            res.send(errMsg)
        } else {
            Sms.update({telNo: req.params.telNo}, {status: false}, {multi: true}, function (err, n) {
                // console.info(n);
                var sms = new Sms();
                var Num = "";
                for (var i = 0; i < 4; i++) {
                    Num += Math.floor(Math.random() * 10);
                }
                sms.smsContont = Num;
                sms.telNo = req.params.telNo;
                sms.smsType = '1';
                sms.status = true;
                sms.invalidTime = moment().add(0.5, 'hours')
                sms.save(function (err) {
                    if (err) {
                        res.send(errMsg)
                    } else {
                        smsSender.send_valid_code(req.params.telNo, Num, function (err, response) {
                            if (err) {
                                res.send(errMsg)
                            } else {
                                res.send(successMsg)
                            }
                        })
                    }
                });
            })
        }
    });
});

router.get('/valid/:telNo/:code', function (req, res, next) {
    var Sms = db.models.Sms;
    var ConsultRecord = db.models.ConsultRecord;
    db.models.Sms.find({
        telNo: req.params.telNo,
        status: true,
        invalidTime: {"$gte": new Date()}
    }).limit(1).exec(function (err, sms) {
        // console.info(sms);
        if (sms.length > 0) {
            if (sms[0].smsContont == req.params.code) {
                mailer.sendMail(req.params.telNo, function (info) {
                    console.info(info);
                    Sms.update({telNo: req.params.telNo}, {status: false}, {multi: true}, function (err, n) {
                    });
                   var consultRecord = new ConsultRecord();
                    consultRecord.telNo = req.params.telNo;
                    consultRecord.status = '1';
                    consultRecord.remark = '请求在线咨询';
                    consultRecord.save(function (err) {

                    });
                    res.send({msg:'验证成功！',err:false})
                })
            } else {
                res.send({msg:'验证失败！',err:true})
            }
        } else {
            res.send({msg:'验证失败！',err:true})
        }
    });
});
module.exports = router;